﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.IO;
using System.Net;
using System.Reflection;
using System.Management;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using System.Security.Cryptography;

namespace BF.Common
{
    /// <summary>
    /// 通用方法类
    /// </summary>
    public class PubCommon
    {

        #region 各系统通用部分

        /// <summary>
        /// 获取系统生成的Key值
        /// </summary>
        public static string GetSysKey
        {
            get
            {
                return Guid.NewGuid().ToString();
            }
        }

        /// <summary>
        /// 转换GB2312 的字符串为UTF8编码
        /// </summary>
        /// <param name="str">待转换的字符串</param>
        /// <returns>转换后的字符串</returns>
        public static string GB2312ToUTF8(string str)
        {
            try
            {
                Encoding uft8 = Encoding.GetEncoding(65001);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                byte[] temp = gb2312.GetBytes(str);
                byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
                string result = uft8.GetString(temp1);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 判断指定的URI是否可用

        /// <summary>
        ///  判断指定的URI是否可用
        /// </summary>
        /// <param name="suri">待测试的URI</param>
        /// <returns>true可用，false不可用</returns>
        public static bool IsUriAvailable(string suri)
        {
            try
            {
                WebRequest webRequest = WebRequest.Create(suri);
                WebResponse webResponse = webRequest.GetResponse();
                return true;
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.Success)
                    return true;
                else
                    return false;
            }
        }

        #endregion

        #region 读写文件

        /// <summary>
        /// 读取待发送的传真文件
        /// </summary>
        /// <param name="filename">文件全名（物理路径+文件名）</param>
        /// <param name="filesize">文件字节数</param>
        /// <returns>返回Base 64 数字编码的文件内容</returns>
        public static string GetFile(string filename, ref int filesize)
        {
            string filetext = "";
            try
            {
                FileStream fs = File.OpenRead(filename);
                filesize = (int)fs.Length;
                BinaryReader br = new BinaryReader(fs);
                filetext = Convert.ToBase64String(br.ReadBytes((int)fs.Length), 0, filesize);
                br.Close();
                fs.Close();
                return filetext;

            }
            catch
            {
                filesize = 0;
                return "";
            }
        }
        /// <summary>
        /// 写传真文件
        /// </summary>
        /// <param name="filename">文件全名（物理路径+文件名）</param>
        /// <param name="filecontent">文件内容</param>
        /// <param name="filesize">文件字节数</param>
        /// <returns>true写传真成功，false写传真失败</returns>
        public static bool WriteFile(string filename, string filecontent, int filesize)
        {
            FileStream fs = null;
            try
            {
                if (!File.Exists(filename))
                {
                    FileInfo fi = new FileInfo(filename);
                    //fi.Delete();
                    fs = File.Open(filename, FileMode.Create, FileAccess.Write);
                    fs.Write(System.Convert.FromBase64String(filecontent), 0, filesize);
                    fs.Close();
                }
                return true;
            }
            catch
            {
                try
                {
                    fs.Close();
                }
                catch
                {
                }
                return false;
            }
        }

        #endregion

        #region 获取短信、传真状态

        /// <summary>
        /// 获取短信状态信息
        /// </summary>
        /// <param name="state">状态码</param>
        /// <returns>短信状态信息</returns>
        public static string GetSmsStateInfo(string state)
        {
            string stateMsg = String.Empty;
            switch (state)
            {

                case "0":
                    stateMsg = "发送成功";//单发时状态“发送成功”群发时为“发送成功进入待发进程”
                    break;
                case "1":
                    stateMsg = "发送成功";//实为群发时“直接发送成功”
                    break;
                case "-1":
                    stateMsg = "帐号未注册";
                    break;
                case "-2":
                    stateMsg = "其他错误";
                    break;
                case "-3":
                    stateMsg = "帐号或者密码错误";
                    break;
                case "-4":
                    stateMsg = "手机号格式不对且一次提交信息不能超过600个手机号码";
                    break;
                case "-5":
                    stateMsg = "余额不足";
                    break;
                case "-6":
                    stateMsg = "定时发送时间不是有效的时间格式";
                    break;
                case "-7":
                    stateMsg = "发送短信内容包含黑字典关键字";
                    break;
                case "-8":
                    stateMsg = "发送内容需在3到250个字之间";
                    break;
                case "-9":
                    stateMsg = "发送号码为空";
                    break;
                default:
                    stateMsg = "短信未发送";
                    break;
            }
            return stateMsg;
        }

        /// <summary>
        /// 获取传真状态信息
        /// </summary>
        /// <param name="state">状态码</param>
        /// <returns>传真状态信息</returns>
        public static string GetFaxStateInfo(string state)
        {
            string stateMsg = String.Empty;
            switch (state)
            {
                case "1":
                    stateMsg = "无法转换";
                    break;
                case "2":
                    stateMsg = "正在同步";
                    break;
                case "3":
                    stateMsg = "同步失败";
                    break;
                case "4":
                    stateMsg = "发送完毕";
                    break;
                case "5":
                    stateMsg = "等待处理";
                    break;
                case "6":
                    stateMsg = "文件不存在";
                    break;
                case "10":
                    stateMsg = "余额不足";
                    break;
                case "20":
                    stateMsg = "等待发送";
                    break;
                case "21":
                    stateMsg = "正在发送";
                    break;
                case "22":
                    stateMsg = "发送失败";
                    break;
                case "23":
                    stateMsg = "未能发送";
                    break;
                case "30":
                    stateMsg = "线路忙  ";
                    break;
                case "31":
                    stateMsg = "无人接听";
                    break;
                case "32":
                    stateMsg = "对方挂断";
                    break;
                case "33":
                    stateMsg = "信号中断";
                    break;
                case "34":
                    stateMsg = "号码未接通";
                    break;
                case "35":
                    stateMsg = "号码不存在";
                    break;
                case "36":
                    stateMsg = "数据不完整";
                    break;
                case "37":
                    stateMsg = "程序异常";
                    break;
                case "40":
                    stateMsg = "失败已重发";
                    break;
                case "41":
                    stateMsg = "等待重发";
                    break;
                case "100":
                    stateMsg = "发送成功";
                    break;

            }
            return stateMsg;
        }

        #endregion

        #region 传真、短信接口建立会话时的XML格式配置文件
        /// <summary>
        /// 调用传真接口时建立会话所需要的XML格式文件
        /// </summary>
        /// <param name="appKey">身份标识</param>
        /// <param name="loginCode">用户类型4终端企业用户、7终端个人用户</param>
        /// <param name="param1">企业编号</param>
        /// <param name="param2">发送传真时接口中的用户名</param>
        /// <param name="param3">发送传真时接口中的密码</param>
        /// <returns>调用传真接口时建立会话所需要的XML格式文件</returns>
        public static string GetFaxXml(string appKey, string loginCode, string param1, string param2, string param3)
        {

            string xml = "<?xml version='1.0' ?>" +
                         "       <DataInfo>" +
                         "         <ActionUserInfo>" +
                         "           <ActionCode>Login</ActionCode>" +
                         "           <SessionID>" + Guid.NewGuid().ToString() + "</SessionID>" +
                         "           <AppKey>" + appKey + "</AppKey>" +
                         "           <LoginCode>" + loginCode + "</LoginCode>" +
                         "           <Param1>" + param1 + "</Param1>" +
                         "           <Param2>" + param2 + "</Param2>" +
                         "           <Param3>" + param3 + "</Param3>" +
                         "         </ActionUserInfo>" +
                         "       </DataInfo>";
            return xml;
        }
        /// <summary>
        /// 调用短信接口时建立会话所需要的XML格式文件
        /// </summary>
        /// <param name="sUserName">短信接口用户名</param>
        /// <param name="sPwd">短信接口密码</param>
        /// <returns>调用短信接口时建立会话所需要的XML格式文件</returns>
        public static string GetSmsXml(string sUserName, string sPwd)
        {
            string xml = "<?xml version='1.0' ?>" +
                        "       <DataInfo>" +
                        "         <UserInfo>" +
                        "           <username>" + sUserName + "</username>" +
                        "           <pwd>" + sPwd + "</pwd>" +
                        "         </UserInfo>" +
                        "       </DataInfo>";
            return xml;
        }
        #endregion

    }
        #endregion

    #region 实体对象拷贝
    /// <summary>
    /// 拷贝实体对象
    /// </summary>
    /// <typeparam name="T1">需要拷贝的实体对象</typeparam>
    /// <typeparam name="T2">拷贝后的新对象</typeparam>
    public class ConverterEntity<T1, T2> where T2 : class, new()
    {
        #region 拷贝实体对象

        /// <summary>
        ///  将指定的实体对象拷贝给指定的实体对象
        /// </summary>
        /// <param name="LocalDataCacheEntity">需要拷贝的实体对象</param>
        /// <param name="ModeilEntity">拷贝后的新对象</param>
        /// <returns>拷贝后的新对象</returns>
        public T2 CoptyEntity(T1 LocalDataCacheEntity, T2 ModeilEntity)
        {
            //获得本地数据对象类型
            Type LocalDataCacheEntityType = typeof(T1);
            //获得转换对象的类型
            Type ModeilEntityType = typeof(T2);
            //循环本地数据对象属性
            foreach (PropertyInfo proPertyInfoLoacal in LocalDataCacheEntityType.GetProperties())
            {
                //循环转换数据对象属性
                foreach (PropertyInfo proPertyInfoModeil in ModeilEntityType.GetProperties())
                {
                    if (proPertyInfoModeil.PropertyType.FullName.Equals("System.Data.EntityState") || proPertyInfoModeil.PropertyType.FullName.Equals("System.Data.EntityKey"))
                    {
                        continue;
                    }

                    //Attribute attr in Attribute.GetCustomAttributes(
                    //判断两对象的属性是否相同
                    if (proPertyInfoLoacal.Name == proPertyInfoModeil.Name)
                    {
                        object[] obje = proPertyInfoLoacal.GetCustomAttributes(false);
                        object value = proPertyInfoLoacal.GetValue(LocalDataCacheEntity, null);
                        if (proPertyInfoLoacal.PropertyType.ToString().Equals("System.String"))
                        {
                            value = string.Empty;
                        }
                        //相同时，将本地数据对象对应的属性值赋值给返回对象
                        proPertyInfoModeil.SetValue(ModeilEntity, value, null);
                    }
                }
            }
            return ModeilEntity;

        }

        #endregion
    }
    #endregion

    #region 通用条件类
    /// <summary>
    /// 通用条件类
    /// </summary>
    public class Condition
    {
        /// <summary>
        /// 数据库表名称
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 当前记录的索引
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; set; }
        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartDate { get; set; }
        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }
        /// <summary>
        /// 提取记录数
        /// </summary>
        public int TopCount { get; set; }
    }
    #endregion
}
